package com.bdth.weget.home.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.bdth.weget.common.MessageResult;
import com.bdth.weget.common.Pagination;
import com.bdth.weget.home.entity.Fleet;
import com.bdth.weget.home.entity.User;
import com.bdth.weget.home.service.FleetService;
import com.bdth.weget.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/7/9.
 */
@Controller
@RequestMapping("/fleet")
public class FleetController extends BaseController {

    @Autowired
    private FleetService fleetService;

    @RequestMapping(value = "index")
    public String index() {
        return "fleet/index";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public PageVo<Fleet> getPageList(HttpServletRequest request, Pagination<Fleet> pagination, @RequestParam(required = false) String name){
        Map<String, Object> query = new HashMap<>();
        query.put("name",name);
        User session = (User)request.getSession().getAttribute("loginUser");
        if(session.getGroupid()!=0) {
            query.put("departids", session.getDepartids());
        }
        Page<Fleet> items = fleetService.listPages(pagination,query);
        PageVo<Fleet> pageVo = new PageVo<>(items.getRecords(),items.getTotal());
        return pageVo;
    }

    @RequestMapping(value = "all")
    @ResponseBody
    public List<Map<String,Object>> getAllList(HttpServletRequest request){
        Map<String, Object> query = new HashMap<>();
        User session = (User)request.getSession().getAttribute("loginUser");
        if(session.getGroupid()!=0) {
            query.put("departids", session.getDepartids());
        }
        List<Fleet> all = fleetService.listAll(query);
        List<Map<String, Object>> result = new ArrayList<>();
        Map<String, Object> root = new HashMap<>();
        root.put("id", 0);
        root.put("text", "请选择");
        result.add(root);
        for (Fleet bean : all){
            Map<String, Object> temp = new HashMap<>();
            temp.put("id", bean.getId());
            temp.put("text", bean.getName());
            result.add(temp);
        }
        return result;
    }

    @RequestMapping(value = "form")
    public String form(){
        return "fleet/form";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public MessageResult save(Fleet param){
        boolean flag = fleetService.save(param);
        if(flag)
            return MessageResult.getSuccess("添加数据成功");
        else
            return MessageResult.getError("500","添加数据失败");
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public MessageResult update(Fleet param){
        boolean flag = fleetService.update(param);
        if(flag)
            return MessageResult.getSuccess("修改数据成功");
        else
            return MessageResult.getError("500","修改数据失败");
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public MessageResult delete(@RequestParam Integer ids){
        boolean flag = fleetService.delete(ids);
        if(flag)
            return MessageResult.getSuccess("删除数据成功");
        else
            return MessageResult.getError("500","删除数据失败");
    }

    @RequestMapping(value = "remove")
    @ResponseBody
    public MessageResult deletes(@RequestParam String ids){
        boolean flag = fleetService.remove(ids);
        if(flag)
            return MessageResult.getSuccess("删除数据成功");
        else
            return MessageResult.getError("500","删除数据失败");
    }

    @RequestMapping(value = "search")
    public String search(){
        return "fleet/search";
    }
}
